package telsoft.app.form;

import java.util.*;

import javax.swing.event.*;

import telsoft.admin.util.*;
import telsoft.comm.protocol.*;
import telsoft.app.util.*;
import telsoft.swing.*;
import javax.swing.JOptionPane;
import telsoft.util.*;
import javax.swing.JButton;
import java.awt.*;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JTextPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class FormReport extends JXFrame implements ControlButtonListener {
    public JXDatePlus StartDate = new JXDatePlus();
    public JXDatePlus FinishDate = new JXDatePlus();
    public JXCombo City = new JXCombo();
    public VectorTable tblValue = new VectorTable();
    public VectorTable tblView = new VectorTable();
    public JButton btnReport = new JButton();
    public JButton btnView = new JButton();
    public JXCheckBox check = new JXCheckBox();
    public JButton btnExit = new JButton();
    public FormReport() throws Exception {
        buildForm();
        StartDate = (JXDatePlus) formData.getField("dtStartDate");
        FinishDate = (JXDatePlus) formData.getField("dtFinishDate");
        City = (JXCombo) formData.getField("City");
        tblValue = (VectorTable) formData.getControl("tblListArea");
	tblView = (VectorTable) formData.getControl("tblListView");
        btnReport = (JButton) formData.getControl("btnReport");
	btnView = (JButton) formData.getControl("btnView");
        btnExit = (JButton) formData.getControl("btnExit");
        check = (JXCheckBox) formData.getControl("check");
        Skin.applySkin(this);
//	onChangeAction(ACTION_NONE, ACTION_NONE);
        City.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                loadForm();
            }
        });
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                exit();
            }
        });
	///view
	btnView.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e){
		String check = (String) City.getSelectedValue();
		String stdate = StartDate.getText();
		String fndate = FinishDate.getText();
		if (check==null) {
		    String strMsg = getDictionary().getString("citynull");
//			MessageBox.showConfirmDialog(FormReport.this, strMsg, Global.APP_NAME,
//					     MessageBox.WARNING_MESSAGE);
		    JOptionPane.showConfirmDialog(null,strMsg,"Cảnh báo",JOptionPane.YES_OPTION,JOptionPane.WARNING_MESSAGE);
		}
		else if(stdate.equals("")){
		    String wstrdate = getDictionary().getString("wstrdate");
		    JOptionPane.showConfirmDialog(null,wstrdate,"Cảnh báo",JOptionPane.YES_OPTION,JOptionPane.WARNING_MESSAGE);
		}
		else if(fndate.equals("")){
		    String wfndate = getDictionary().getString("wfndate");
		    JOptionPane.showConfirmDialog(null,wfndate,"Cảnh báo",JOptionPane.YES_OPTION,JOptionPane.WARNING_MESSAGE);
		}
		 else if(check.equals("ALL")){
		     ViewReportAll();
		}
		else {
		    ViewReport();
		}
	  }
	});
	////
        btnReport.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String check = (String) City.getSelectedValue();
		String stdate = StartDate.getText();
		String fndate = FinishDate.getText();
                if (check==null) {
		    String strMsg = getDictionary().getString("citynull");
//			MessageBox.showConfirmDialog(FormReport.this, strMsg, Global.APP_NAME,
//					     MessageBox.WARNING_MESSAGE);
		    JOptionPane.showConfirmDialog(null,strMsg,"Cảnh báo",JOptionPane.YES_OPTION,JOptionPane.WARNING_MESSAGE);
                }
		else if(stdate.equals("")){
		    String wstrdate = getDictionary().getString("wstrdate");
		    JOptionPane.showConfirmDialog(null,wstrdate,"Cảnh báo",JOptionPane.YES_OPTION,JOptionPane.WARNING_MESSAGE);
		}
		else if(fndate.equals("")){
		    String wfndate = getDictionary().getString("wfndate");
		    JOptionPane.showConfirmDialog(null,wfndate,"Cảnh báo",JOptionPane.YES_OPTION,JOptionPane.WARNING_MESSAGE);
		}
                 else if(check.equals("ALL")){
			         ReportAll();
		    }else{
			Report();
                    }
              //  }
            }
        });
	loadcity();
    }

    public void loadForm() {
        try {
            DataTransmit Request = new DataTransmit();
            Request.setString("mstrCity", (String) City.getSelectedValue());
            DataTransmit prmResponse = AppClient.sendRequest(
                    "telsoft.app.bean.ReportBean",
                    "loadForm", Request);
            Vector vtResult = (Vector) prmResponse.getReturn();
            int iSelected = tblValue.getSelectedRow();
            tblValue.setData(vtResult);
            if (tblValue.getRowCount() > 0) {

                if (iSelected < 0 || iSelected >= tblValue.getRowCount()) {
                    iSelected = 0;
                }
                tblValue.setRowSelectionInterval(iSelected, iSelected);
            }
//	    checkControlButton();
        } catch (Exception e) {
            e.printStackTrace();
            MessageBox.showMessageDialog(this, e, Global.APP_NAME,
                                         MessageBox.ERROR_MESSAGE);
        }
    }
public void loadcity() throws Exception {
    DataTransmit prmRequest = new DataTransmit();
    DataTransmit prmResponse = AppClient.sendRequest(
		    "telsoft.app.bean.ReportBean",
		    "loadcity", prmRequest);
	    Vector vtRow = (Vector) prmResponse.getReturn();
	    Vector vtE = new Vector();
	    vtE.addElement("ALL");
	    vtE.addElement("Tất cả thành phố");
	    vtRow.addElement(vtE);
	    if(vtRow!=null){
		City.fillValue(vtRow,1,false);
	    }
}
//    public Vector getID(){
//	Vector id = new Vector();
//	int count = tblValue.getRowCount();
//	for(int i = 0;i<count;i++)
//	{
//	    if(tblValue.getRow(i).elementAt(2).equals("true")){
//		id.addElement(tblValue.getRow(i).elementAt(0));
//	    }
//	}
//	return id;
//    }
    public String getID() {
        String area_id = "";
        int count = tblValue.getRowCount();
        for (int i = 0; i < count; i++) {
            if (tblValue.getRow(i).elementAt(2).equals("true")) {
                area_id = area_id + tblValue.getRow(i).elementAt(0) + ",";
            }
        }
        area_id = area_id.substring(0, area_id.length() - 1);
        return area_id;
    }
//ViewReport
    public void ViewReport(){
	DataTransmit request = new DataTransmit();
	request.setString("mstrArea_id", getID());
	request.setString("mstrCity", (String) City.getSelectedDisplay());
	request.setString("mstrStartdate", StartDate.getText().trim());
	request.setString("mstrFinishdate", FinishDate.getText().trim());
	try{
            DataTransmit response = AppClient.sendRequest(
                    "telsoft.app.bean.ReportBean", "ViewReport", request);
            Vector vtRow = (Vector) response.getReturn();
            Vector vtData = new Vector();
            tblView.setData(vtRow);
        }
	catch(Exception e){
	    e.printStackTrace();
	}
}
////view report all
    public void ViewReportAll(){
       DataTransmit request = new DataTransmit();
//       request.setString("mstrArea_id", getID());
       request.setString("mstrCity", (String) City.getSelectedDisplay());
       request.setString("mstrStartdate", StartDate.getText().trim());
       request.setString("mstrFinishdate", FinishDate.getText().trim());
       try{
	   DataTransmit response = AppClient.sendRequest(
		   "telsoft.app.bean.ReportBean", "ViewReportAll", request);
	   Vector vtRow = (Vector) response.getReturn();
	   Vector vtData = new Vector();
	   tblView.setData(vtRow);
       }
       catch(Exception e){
	   e.printStackTrace();
       }
}

/////
    public void Report() {
        DataTransmit request = new DataTransmit();
        request.setString("mstrArea_id", getID());
        request.setString("mstrCity", (String) City.getSelectedDisplay());
        request.setString("mstrStartdate", StartDate.getText().trim());
        request.setString("mstrFinishdate", FinishDate.getText().trim());
        request.setString("In.Template", "ReportPTTB.xls");
        try {
            DataTransmit response = AppClient.sendRequest(
                    "telsoft.app.bean.ReportBean", "Report", request);
            String strFileName = response.getString("Out.strFileNameOut");
            String strURL = AppClient.getSessionValue("Link.Report");
            if (!strURL.endsWith("/")) {
                strURL += "/";
            }
            UrlUtil.openURL(null, strURL + strFileName);

        } catch (Exception ex) {
            MessageBox.showMessageDialog(this, ex.getMessage(),
                                         Global.APP_NAME);
        }
    }
    public void ReportAll() {
	    DataTransmit request = new DataTransmit();
//	    request.setString("mstrArea_id", getID());
	    request.setString("mstrCity", (String) City.getSelectedDisplay());
	    request.setString("mstrStartdate", StartDate.getText().trim());
	    request.setString("mstrFinishdate", FinishDate.getText().trim());
	    request.setString("In.Template", "ReportPTTB.xls");
	    try {
		DataTransmit response = AppClient.sendRequest(
			"telsoft.app.bean.ReportBean", "ReportAll", request);
		String strFileName = response.getString("Out.strFileNameOut");
		String strURL = AppClient.getSessionValue("Link.Report");
		if (!strURL.endsWith("/")) {
		    strURL += "/";
		}
		UrlUtil.openURL(null, strURL + strFileName);

	    } catch (Exception ex) {
		MessageBox.showMessageDialog(this, ex.getMessage(),
					     Global.APP_NAME);
	    }
    }
    public String getPermission() {
        return telsoft.admin.util.AppClient.getPermission(PermissionApp.
                FormReport);

    }

    public boolean add() {
        return false;
    }

    public boolean modify() {
        return false;
    }

    public boolean remove() {
        return false;
    }

    public boolean search() {
        return false;
    }

    public boolean validateInput(int _int, int _int1) {
        return false;
    }

    public void onChangeAction(int _int, int _int1) {
    }
}
